package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取会员学生（家长学生）信息
func GetUserStudentInfo(p map[string]interface{}) *models.UserStudent {
	o := orm.NewOrm()
	userStudent := &models.UserStudent{}
	qs := o.QueryTable("UserStudent")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	if _, ok := p["studentId"]; ok {
		qs = qs.Filter("student_id", p["studentId"])
	}
	qs.One(userStudent)
	return userStudent
}

// 添加会员学生（家长学生）信息
func AddUserStudent(input url.Values) (int64, error) {
	o := orm.NewOrm()
	userStudent := &models.UserStudent{}

	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	userStudent.User = user

	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	userStudent.Student = student

	return o.Insert(userStudent)
}
